﻿/**	std_functional.hpp
 *	----	Create Time Stamp	--2007-06-02 01:12:38--
 *------------------------------------------------------------------------------
 *	Mean	:
 *	Author	:Nick Shallery	(nicknide@gmail.com)
 *	Version	:1.0.4.5	(2009-07-29 00:32:55)
 *------------------------------------------------------------------------------
**/	/*YOU HAVE ALL LEFT WITH THIS CODE WHILE YOU KEEP COMMENT IN EVERY COPY.*/

#if			!defined(__LOLIX__TOY__STD_FUNCTIONAL__NICK__K_IN_G__)
#define		__LOLIX__TOY__STD_FUNCTIONAL__NICK__K_IN_G__
//==============================================================================
//--------------------------------------------------------------------------


namespace lolix{namespace toy{

template<class _Arg, class _Result>
struct unary_function
{	// base class for unary functions
	typedef _Arg argument_type;
	typedef _Result result_type;
};

template<class _Arg1,
class _Arg2,
class _Result>
struct binary_function
{	// base class for binary functions
	typedef _Arg1 first_argument_type;
	typedef _Arg2 second_argument_type;
	typedef _Result result_type;
};

class	release_itf_proc
	: public unary_function<itf*, size_type>
{
public:
	//typedef	lolix::itf*	argument_type;
	//typedef	void		result_type;
	size_type	operator ()(itf* in_ptr)const
	{
		if ( in_ptr )
			return in_ptr->release();
		return 0;
	}
};


template<typename _Ty1, typename _Ty2>
class	bind_invoke_function
	: public unary_function<typename _Ty1::argument_type, typename _Ty2::result_type>
{
	_Ty1	_ty1;
	_Ty2	_ty2;
public:
	bind_invoke_function<_Ty1, _Ty2>(const _Ty1& t1, const _Ty2& t2): _ty1(t1), _ty2(t2){}
	result_type	operator()(argument_type& pt)const
	{
		return _ty2(_ty1(pt));
	}
};

template<typename _Ty1, typename _Ty2>
bind_invoke_function<_Ty1, _Ty2> make_bind_invoke(const _Ty1& ty1, const _Ty2& ty2)
{
	return bind_invoke_function<_Ty1, _Ty2>(ty1, ty2);
}


template<class _R, class _T, int = 0>
class mem_fun_t;
template<class _R, class _T, class _A, int = 0>
class mem_fun1_t;
template<class _R, class _T, int = 0>
class mem_fun_const_t;

#define		__LOLIX_TMP__DEF__MEM_FUNC_T(CallName, CallNumber)				\
template<class _R, class _T> inline											\
mem_fun_t<_R, _T, CallNumber> mem_fun(_R (CallName _T::*_p)(void))			\
{ return mem_fun_t<_R, _T, CallNumber>(_p); }								\
																			\
template<class _R, class _T> inline											\
mem_fun_const_t<_R, _T, CallNumber> mem_fun(_R (CallName _T::*_p)(void)const)	\
{ return mem_fun_const_t<_R, _T, CallNumber>(_p); }							\
																			\
template<class _R, class _T>												\
class mem_fun_t<_R, _T, CallNumber>											\
	: public unary_function<_T*, _R>										\
	{	/* functor adapter (*p->*pfunc)(), non-const *pfunc	*/				\
public:																		\
	explicit mem_fun_t(_R (CallName _T::*_Pm)(void))						\
		: _Pmemfun(_Pm)														\
		{	/* construct from pointer	*/									\
		}																	\
	_R operator()(_T *_Pleft) const											\
		{	/* call function	*/											\
		return (_Pleft->*_Pmemfun)();										\
		}																	\
private:																	\
	_R (CallName _T::*_Pmemfun)(void);	/* the member function pointer	*/	\
	};																		\
																			\
template<class _R, class _T>												\
class mem_fun_const_t<_R, _T, CallNumber>									\
	: public unary_function<_T*, _R>										\
	{	/* functor adapter (*p->*pfunc)(), non-const *pfunc	*/				\
public:																		\
	explicit mem_fun_const_t(_R (CallName _T::*_Pm)(void)const)				\
		: _Pmemfun(_Pm)														\
		{	/* construct from pointer	*/									\
		}																	\
	_R operator()(_T *_Pleft) const											\
		{	/* call function	*/											\
		return (_Pleft->*_Pmemfun)();										\
		}																	\
private:																	\
	_R (CallName _T::*_Pmemfun)(void)const;	/* the member function pointer	*/	\
	};																		\
template<class _R, class _T, class _A> inline								\
mem_fun1_t<_R, _T, _A, CallNumber> mem_fun1(_R (CallName _T::*_p)(_A))		\
{ return mem_fun1_t<_R, _T, _A, CallNumber>(_p); }							\
/* TEMPLATE CLASS mem_fun1_t */												\
template<class _R, class _T, class _A>										\
class mem_fun1_t<_R, _T, _A, CallNumber>									\
	: public binary_function<_T*, _A, _R>									\
	{	/* functor adapter (*p->*pfunc)(val), non-const *pfunc	*/			\
public:																		\
	explicit mem_fun1_t(_R (CallName _T::*_Pm)(_A))							\
		: _Pmemfun(_Pm)														\
		{	/* construct from pointer */									\
		}																	\
																			\
	_R operator()(_T *_Pleft, _A _Right) const								\
		{	/* call function with operand */								\
		return ((_Pleft->*_Pmemfun)(_Right));								\
		}																	\
																			\
private:																	\
	_R (CallName _T::*_Pmemfun)(_A);/* the member function pointer */		\
	}

#if			LOLIX_CFG__SIZE_TYPE_BITS_SIZE <= 32 && !defined(_M_CEE)
	__LOLIX_TMP__DEF__MEM_FUNC_T(__thiscall, 0);
	__LOLIX_TMP__DEF__MEM_FUNC_T(__cdecl, 1);
	__LOLIX_TMP__DEF__MEM_FUNC_T(__stdcall, 2);
	__LOLIX_TMP__DEF__MEM_FUNC_T(__fastcall, 3);
#else	//	LOLIX_CFG__SIZE_TYPE_BITS_SIZE <= 32 && !defined(_M_CEE)
	__LOLIX_TMP__DEF__MEM_FUNC_T(, 0);
#endif	//	LOLIX_CFG__SIZE_TYPE_BITS_SIZE <= 32 && !defined(_M_CEE)

#undef		__LOLIX_TMP__DEF__MEM_FUNC_T

class	less
{
public:
	//typedef	_Tl	first_argument_type;
	//typedef	_Tr	second_argument_type;
	//typedef	bool	result_type;

	template<typename _Tl, typename _Tr>
	LOLIX_INLINE_CALL	bool operator ()(const _Tl& tl, const _Tr& tr)const
		{
		return tl < tr;
		}
};

}}


//--------------------------------------------------------------------------
//==============================================================================
#endif	//	!defined(__LOLIX__TOY__STD_FUNCTIONAL__NICK__K_IN_G__)


